/**
 * 2018年11月27日上午9:19:33
 * @author 谢进财
 */
package com.baihang.project.ds.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baihang.framework.controller.BaseController;
import com.baihang.project.ds.common.RemoteInvocationKit;
import com.baihang.project.ds.service.MySqlRedisService;
import com.baihang.project.ds.service.SystemConfigService;

/**
 * 系统配置
 *
 */
@Controller
@RequestMapping("/systemConfig")
public class SystemConfigController extends BaseController {
		@Autowired
		private SystemConfigService service;
		@Autowired
		private MySqlRedisService mRservice;
		@RequestMapping
		public String index() {
						
			return "/project/ds/systemConfig/index";
		}
		
		@RequestMapping(value="/getConfig",produces="application/json;charset=UTF-8")
		@ResponseBody
		public String getConfig() {
			String result = service.getConfig();
			System.err.println(result);
			return result;
		}
		/**
		 * 改变项目状态
		 * @param state
		 * @return
		 */
		@RequestMapping(value="/changeState",produces="application/json;charset=UTF-8")
		@ResponseBody
		public String changeState(String state) {
			service.changeState(state);//改状态
			//修改redis配置
			service.changeRedisState(state);			
			//使微信端向前端推送
			try {
				RemoteInvocationKit.houseStateControl(state);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if("4".equals(state)) {//项目启动
				mRservice.startFromMysqlToRedis();//进行mysql与redis同步
			}
			String result = service.getConfig();
			System.err.println(result);
			return result;
		}
		
		@RequestMapping(value="/save",produces="application/json;charset=UTF-8")
		@ResponseBody
		public String save(String json) {
			service.save(json);	
			service.redisSave(json);
			String result = service.getConfig();
			return result;				
		}
		
		@RequestMapping(value="/ClearRoom",produces="application/json;charset=UTF-8")
		@ResponseBody
		public String ClearRoom() {
			service.ClearRoom();
			String result = service.getConfig();
			return result;
		}
		
		@RequestMapping(value="/clearAndSave",produces="application/json;charset=UTF-8")
		@ResponseBody
		public String clearAndSave(String json) {
			service.ClearRoom();
			service.save(json);
			service.redisSave(json);
			String result = service.getConfig();
			RemoteInvocationKit.resetClien();
			return result;
		}
		
		@RequestMapping(value="/clearHouse",produces="application/json;charset=UTF-8")
		@ResponseBody
		public String clearHouse() {
			service.clearHouse();			
			String result = service.getConfig();
			return result;
		}
		
}
